﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.IsolatedStorage;
using System.IO;
using System.Reflection;
using System.Collections;
using BussinesLogic;

namespace Foliatura
{
    public partial class DocumentBrowser : Form
    {
        private ArrayList Expedientes;
        private BussinesLogic.Expediente selectedExpediente;
        private BussinesLogic.Documento selectedDocumento;
        private Controller controlador;
        private int selectedPage;

        public DocumentBrowser()
        {
            this.controlador = Controller.Instance();
            InitializeComponent();
            initBrowser();
            initInitialData();
        }

        public void initBrowser()
        {
            webBrowser.DocumentText = Foliatura.Properties.Resources.index.ToString();
        }

        private void initInitialData()
        {
            this.Expedientes =
                BussinesLogic.Expediente.getExpedientes();

            ArrayList datamembers = new ArrayList();

            datamembers.Add(new string[] { "NoExpediente", "No. Expediente" });

            datamembers.Add(new string[] { "Descripcion", "Descripción" });

            this.lstExpedientes.DataMemberForList = datamembers;
            this.lstExpedientes.DataSource = this.Expedientes;

            this.lblUserName.Text = this.controlador.user.Nombre1 + " " + this.controlador.user.Apellido1;
            this.lnlDepartamento.Text = this.controlador.user.Departamento;
        }

        private void selectExpediente(string codExpediente)
        {
            foreach (BussinesLogic.Expediente expediente in this.Expedientes)
            {
                if (expediente.NoExpediente == codExpediente)
                {
                    this.selectedExpediente = expediente;
                    break;
                }
            }

            if (this.selectedExpediente != null)
            {
                this.lstDocumentos.DataSource = this.selectedExpediente.Documentos;

                this.lstDocumentos.DisplayMember = "CodDocumento";

                this.lstDocumentos.ValueMember = "id";

            }
        }

        private void btnIrNumExpediente_Click(object sender, EventArgs e)
        {
            selectExpediente(this.txtNumExpediente.Text);
        }

        private void lstExpedientes_DoubleClick(object sender, EventArgs e)
        {
            Expediente tmpEx = (Expediente)this.lstExpedientes.SelectedItemFromList;

            selectExpediente(tmpEx.NoExpediente);
        }

        private void lstDocumentos_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (this.lstDocumentos.Items.Count > 0)
                {
                    this.selectedDocumento = (BussinesLogic.Documento)
                        this.lstDocumentos.SelectedItem;
                    Object[] objArray = new Object[1];
                    objArray[0] = (Object)this.selectedDocumento.getHML(1);
                    webBrowser.Document.InvokeScript("changeBody", objArray);

                    this.lblPagNo.Text = "Página No: 1";
                    this.selectedPage = 1;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void scroolToAnchor(string anchor)
        {
            HtmlElementCollection elements = this.webBrowser.Document.Body.All;
            foreach (HtmlElement element in elements)
            {
                string nameAttribute = element.GetAttribute("Name");
                if (!string.IsNullOrEmpty(nameAttribute) && nameAttribute == anchor)
                {
                    element.ScrollIntoView(true);
                    break;
                }
            }
        }

        private void btnIrNumPagina_Click(object sender, EventArgs e)
        {
            if (this.selectedDocumento != null)
            {
                try
                {
                    int pageNo = Convert.ToInt32(this.txtNumPagina.Text);
                    int numFolio = this.selectedDocumento.getFolioNumForPage(pageNo);

                    Object[] objArray = new Object[1];
                    objArray[0] = (Object)this.selectedDocumento.getHML(numFolio);
                    webBrowser.Document.InvokeScript("changeBody", objArray);

                    this.scroolToAnchor("linea" + pageNo);

                    this.lblPagNo.Text = "Página No: " + numFolio;
                    this.selectedPage = numFolio;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void goToPage(int page)
        {
            if (this.selectedDocumento == null)
            {
                return;
            }
            Object[] objArray = new Object[1];
            objArray[0] = (Object)this.selectedDocumento.getHML(page);
            webBrowser.Document.InvokeScript("changeBody", objArray);

            this.lblPagNo.Text = "Página No: " + page;
        }

        private void btnless_Click(object sender, EventArgs e)
        {
            this.selectedPage--;
            if (this.selectedPage <= 0)
            {
                this.selectedPage = 1;
                return;
            }
            goToPage(this.selectedPage);
        }

        private void btnpluss_Click(object sender, EventArgs e)
        {
            this.selectedPage++;
            if (this.selectedPage > this.selectedDocumento.Folios.Count)
            {
                this.selectedPage = this.selectedDocumento.Folios.Count;
                return;
            }

            goToPage(this.selectedPage);
        }        
    }
}
